USE SAS
GO

 IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'InsUpdPartner')
	BEGIN
		DROP  Procedure  dbo.InsUpdPartner
	END
GO

CREATE Procedure dbo.InsUpdPartner (
	@PARTNERNO				VARCHAR(10)		= '0'	OUTPUT
,	@PARTNERGUID			VARCHAR(100)
,	@PARTNERCATEGORY		VARCHAR(10)
,	@PARTNERTYPE			VARCHAR(10)
,	@USERID					VARCHAR(10)				
,	@PARTNERNAME			VARCHAR(200)	= NULL
,	@PARTNERLASTNAME		VARCHAR(200)	= NULL
,	@PARTNERFIRSTNAME		VARCHAR(200)	= NULL
,	@PARTNERDIVISION		VARCHAR(10)		= '1'
,	@ACCOUNTGROUP			VARCHAR(10)		= NULL
,	@DATAORIGIN				VARCHAR(10)		= NULL
,	@VERTICAL				VARCHAR(10)		= NULL
,	@ISACTIVE				VARCHAR(10)		= 'X'
,	@WEBSITE				VARCHAR(200)	= NULL
,	@WEBSITEDESC			VARCHAR(200)	= NULL
,	@NOOFLOCATIONS			VARCHAR(10)		= NULL
)
AS
BEGIN

	SET @ISACTIVE = CASE WHEN @ISACTIVE IN ('X','1') THEN '1' ELSE '0' END

	IF EXISTS(SELECT 1 FROM TR_PARTNER WHERE PartnerGUID = PartnerGUID) 
	BEGIN		-- UPDATE
		UPDATE TR_PARTNER
		SET	PartnerName					=	@PartnerName
		,	PartnerLastName				=	@PartnerLastName
		,	PartnerFirstName			=	@PartnerFirstName
		,	PartnerDivision				=	@PartnerDivision
		,	AccountGroup				=	@AccountGroup
		,	DataOrigin					=	@DataOrigin
		,	Vertical					=	@Vertical
		,	isActive					=	@isActive
		,	ChangedBy					=	@USERID
		,	ChangeDate					=	GETDATE()
		,	Website						=	@Website
		,	WebsiteDesc					=	@WebsiteDesc
		,	NoofLocations				=	@NoofLocations
		WHERE
			CAST(PartnerGuid AS VARCHAR(40)) =	@PartnerGuid
	END
	ELSE
	BEGIN		-- INSERT

		-- INSERT ACTIVITY
		SELECT @PARTNERNO		= ISNULL(MAX(PartnerNo), 0) +1 FROM TR_PARTNER

		-- PARTNER

		INSERT INTO TR_PARTNER (PartnerNo,PartnerGuid,PartnerCategory,PartnerType,PartnerName,PartnerLastName,
								PartnerFirstName,PartnerDivision,AccountGroup,DataOrigin,Vertical,isActive,
								CreatedBy,CreateDate,ChangedBy,ChangeDate,Website,WebsiteDesc,NoofLocations)
		VALUES(
			@PartnerNo
		,	@PartnerGuid
		,	@PartnerCategory
		,	@PartnerType
		,	@PartnerName
		,	@PartnerLastName
		,	@PartnerFirstName
		,	@PartnerDivision
		,	@AccountGroup
		,	@DataOrigin
		,	@Vertical
		,	@isActive
		,	@USERID
		,	GETDATE()
		,	@USERID
		,	GETDATE()
		,	@Website
		,	@WebsiteDesc
		,	@NoofLocations
		)
	END
END
GO
